package org.laobai.meiqia.skill.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    @Bean("queue")
    public Queue queue() {
        Map<String, Object> arguments = new HashMap<>();
        // 交换机 名字
        //arguments.put("x-dead-letter-exchange","dead_exchange");
        // 路由key
        //arguments.put("x-dead-letter-routing-key","");
        //arguments.put("x-message-ttl",1000*60*10);
        return new Queue("meiqia.skill.order", false, false, false, arguments);
    }

    @Bean("delayQueue")
    public Queue delayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        // 交换机 名字
        arguments.put("x-dead-letter-exchange", "meiqia.skill.dead.order.exchange");
        // 路由key
        arguments.put("x-dead-letter-routing-key", "");
        arguments.put("x-message-ttl", 1000 * 60);
        return new Queue("meiqia.skill.delay.order", false, false, false, arguments);
    }

    @Bean(name = "delayExchange")
    public FanoutExchange delayExchange() {
        return new FanoutExchange("meiqia.skill.delay.order.exchange", false, false);
    }
    @Bean
    public Binding delayBind(@Qualifier("delayQueue")Queue queue,@Qualifier("delayExchange") Exchange exchange){
        return  BindingBuilder.bind(queue).to(exchange).with("").and(null);
    }

    @Bean(name = "deadQueue")
    public Queue deadQueue() {
        return new Queue("meiqia.skill.dead.order", false);
    }

    @Bean(name = "deadExchange")
    public FanoutExchange deadExchange() {
        return new FanoutExchange("meiqia.skill.dead.order.exchange",false,false);
    }

    @Bean("exchange")
    public Exchange exchange() {
        return new FanoutExchange("meiqia.skill.order.exchange", false, false, null);
    }

    @Bean
    public Binding binding(@Qualifier("queue") Queue queue, @Qualifier("exchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").and(null);
    }

    @Bean
    public Binding bindingDead(@Qualifier("deadQueue") Queue queue, @Qualifier("deadExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").and(null);
    }


    // 手动确认模式
    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        /*
         * AcknowledgeMode.NONE：自动确认
         * AcknowledgeMode.AUTO：根据情况确认
         * AcknowledgeMode.MANUAL：手动确认
         */
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }
}
